package zkk.com.mengqu.manager;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import zkk.com.mengqu.service.ApiService;
import zkk.com.mengqu.service.TieTuKuService;

/**
 * Created by Administrator on 2016/6/27 0027.
 * 初始化网络接口
 */
public class APIManager {
    //服务器地址
//    public static String url = "http://114.80.246.158:1000/";
//    public static String url = "http://119.23.9.75/";
    public static String url = "http://192.168.3.121:8080/";


    //本App采用一个service的方法
    public static ApiService setManager(){
        Retrofit retrofit = new Retrofit.Builder()
                .client(getOkhttpCloent())
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService;
    }

    public static TieTuKuService tieTUkuManager(){
        Retrofit retrofit = new Retrofit.Builder()
                .client(getOkhttpCloent())
                .baseUrl("http://up.imgapi.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        TieTuKuService tieTuKuService = retrofit.create(TieTuKuService.class);
        return tieTuKuService;
    }

    //根据不同的service---多service
//    public static <S> S setManager(Class<S> serviceClass){
//        Retrofit retrofit = new Retrofit.Builder()
//                .client(getOkhttpCloent())
//                .baseUrl(url)
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .build();
////        ApiService apiService = ;
//        return retrofit.create(serviceClass);

    //添加拦截器-打印请求信息（完整url）和回传值
    public static OkHttpClient getOkhttpCloent(){
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor)
                .connectTimeout(1000*30, TimeUnit.SECONDS)//设置连接超时时间30秒
//                .cache(new Cache(new File("路径"),缓存大小))//设置缓存1024*1024*20;//缓存文件最大限制大小20M 最小单位是Byte
                .build();

        return client;
    }
}
